package Bean;
public class LinkList {
	
	private node head, end, current;

	public LinkList() {
		head = end = current = null;
	}

	public void print() {
		if (head == null) {
			System.out.println("The list is empty!");
			System.exit(0);
		} else {
			current = head;
			while (current != null) {
				System.out.print(current.data + " ");
				current = current.next;
			}
		}
	}

	public void insertTail(int d) {
		node newnode = new node(d, null);
		if (head == null) {
			head = newnode;
			end = newnode;
			newnode.next = null;
		} else {
			end.next = newnode;
			end = newnode;
			end.next = null;
		}
	}

	public void insertHead(int d) {
		node newnode = new node(d, null);
		if (head == null) {
			head = newnode;
			end = newnode;
			newnode.next = null;
		} else {
			newnode.next = head;
			head = newnode;
		}
	}

	public int length() {
		if (head == null)
			return 0;
		else {
			int len = 0;
			current = head;
			while (current != null) {
				len++;
				current = current.next;
			}
			return len;
		}
	}

	public int search(int d) {
		int index;
		if (head == null) {
			System.out.println("Search in a empty list is illeagal!");
			System.exit(1);
		} else {
			index = 0;
			current = head;
			while (current != null) {
				index++;
				if (current.data == d)
					break;
				current = current.next;
			}
			if (current.data == d)
				return index - 1;
			else if (current == null)
				return -1;
		}
		return -1;
	}
}